// pages/andon/tag_bind/tag_bind.js
const behavior = getApp().require("behaviors/requests.js")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [behavior],
  data: {
    flag: true,
    factories: ["西区家用总装", "东区家用总装", "商用总装", "中山基地"],
    lines: [],
    line: [],
    factoryPicker: false,
    linePicker: false,
    two_bind_tag_id_line: "",
    two_bind_tag_id_car: "",
    two_bind_factory: "",
    two_bind_line: "",
    two_bind_material_name1: "",
    two_bind_material_name2: "",
    two_bind_material_name3: "",
    two_bind_close_loop_time: "",

    three_bind_tag_id_line: "",
    three_bind_tag_id_person: "",
    three_bind_tag_id_car: "",
    three_bind_factory: "",
    three_bind_line: "",
    three_bind_material_name1: "",
    three_bind_material_name2: "",
    three_bind_material_name3: "",
    three_bind_close_loop_time: ""
  },
  setFlagTrue() {
    this.setData({
      flag: true,
      line: [],
      two_bind_factory:""
    })
  },
  setFlagFalse() {
    this.setData({
      flag: false,
      line: [],
      three_bind_factory:""
    })
  },
  showFactoryPicker() {
    this.setData({
      factoryPicker: true
    })
  },
  showLinePicker() {
    this.setData({
      linePicker: true
    })
  },
  onConfirmFactory(event) {
    const { value, index } = event.detail;
    const url1 = 'https://logisticsengineering.gree.com/andon_system/get_line_list';
    const data = { "factory": value };
    const that = this;
    this.get(url1, data).then((res) => {
      console.log(res)
      that.setData({
        line: res.data
      })
      console.log(that.data.line)
    });
    if (this.data.flag) {
      this.setData({
        "two_bind_factory": value,
        factoryPicker: false
      });
      console.log(this.data.line);
    }
    else {
      this.setData({
        "three_bind_factory": value,
        factoryPicker: false
      })
    }
  },
  onConfirmLine(event) {
    const value = event.detail.value
    if (this.data.flag) {
      this.setData({
        "two_bind_line": value,
        linePicker: false
      })
    }
    else {
      this.setData({
        "three_bind_line": value,
        linePicker: false
      })
    }
  },
  cancel_factoryPicker() {
    this.setData({
      factoryPicker: false
    })
  },
  cancel_linePicker() {
    this.setData({
      linePicker: false
    })
  },
  getLines() {
    const that = this;
    const url1 = 'https://logisticsengineering.gree.com/andon_system/get_west_line_list';
    const url2 = 'https://logisticsengineering.gree.com/andon_system/get_east_line_list';
    const url3 = 'https://logisticsengineering.gree.com/andon_system/get_ShangY_line_list';
    const url4 = 'https://logisticsengineering.gree.com/andon_system/get_ZhongS_line_list';
    this.get(url1).then((res) => {
      that.setData({
        'lines[0]': res.data
      })
    }).catch((err) => {
      console.log(err)
    })
    this.get(url2).then((res) => {
      that.setData({
        'lines[1]': res.data
      })
    }).catch((err) => {
      console.log(err)
    })
    this.get(url3).then((res) => {
      that.setData({
        'lines[2]': res.data
      })
    }).catch((err) => {
      console.log(err)
    })
    this.get(url4).then((res) => {
      that.setData({
        'lines[3]': res.data
      })
    }).catch((err) => {
      console.log(err)
    })
  },

  onSubmitTwo() {
    const that = this;
    const url = 'https://logisticsengineering.gree.com/andon_system/two_binding';
    const data = {
      data: {
        tag_id_line: that.data.two_bind_tag_id_line,
        tag_id_car: that.data.two_bind_tag_id_car,
        factory: that.data.two_bind_factory,
        line: that.data.two_bind_line,
        material_name1: that.data.two_bind_material_name1,
        material_name2: that.data.two_bind_material_name2,
        material_name3: that.data.two_bind_material_name3,
        close_loop_time: that.data.two_bind_close_loop_time,
      }
    };
    this.get(url, data).then((res) => {
      console.log(res)
      if (res.code == 20000) {
        wx.showToast({
          title: '绑定成功',
        })
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
    }).catch((err) => {
      console.log(err)
    })
  },
  onSubmitThree() {
    const that = this

    const url = 'https://logisticsengineering.gree.com/andon_system/three_binding';
    const data = {
      data: {
        tag_id_line: that.data.three_bind_tag_id_line,
        tag_id_person: that.data.three_bind_tag_id_person,
        tag_id_car: that.data.three_bind_tag_id_car,
        factory: that.data.three_bind_factory,
        line: that.data.three_bind_line,
        material_name1: that.data.three_bind_material_name1,
        material_name2: that.data.three_bind_material_name2,
        material_name3: that.data.three_bind_material_name3,
        close_loop_time: that.data.three_bind_close_loop_time
      }
    }
    this.get(url, data).then((res) => {
      console.log(res)
      if (res.code == 20000) {
        wx.showToast({
          title: '绑定成功',
        })
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
    }).catch((err) => {
      console.log(err)
    })
  },
  getFactories() {
    const that = this;
    const url = "https://logisticsengineering.gree.com/andon_system/get_factory_list";
    this.get(url).then((res) => {
      that.setData({
        factories: res.data
      })
      console.log(res)
    }).catch((err) => {
      console.log(err)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getLines();
    this.getFactories();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})